<template>
  <!-- 上传厂家证照的表格modal -->
  <div>
    <basic-modal 
        :modalOpt="modal" 
        :buttonText="['','取消']"
        @closeModal="closeModal" 
        :footer="footer" 
        >
      <template #cusBtn>
          <a-button type="primary" :loading="loading_p" @click="onPassed" >{{isview?'确认':'批量确认'}}</a-button>
          <a-button type="primary"  @click="onUnPassed" >{{isview?'拒绝':'批量拒绝'}}</a-button>
      </template>
      <template>
        <p v-if="isbatch">操作后请提醒客户查看！</p>
        <batch-operat-btn v-if="editable" :isView="false"  :addData="true" @onAddData="onAddData">
          <template #cusBtn> </template>
        </batch-operat-btn>
        <a-list-info v-if="isview" :infoDatas="infoDatas" />
        <div class="recordBox" v-if="isview">
          <a-list-table
            :columns="columns"
            :tableData="listdata"
            :isSelecttion="false"
            :loading="loading"
            :scroll="{ y: null }"
            :renderStateByField="['saleOrderState']"
            :pagination="pagination"
            @onTableChange="handleTableChange"
          >
          </a-list-table>
        </div>
      </template>
    </basic-modal>
    <basic-modal 
        :modalOpt="reason" 
        :buttonText="['','取消']"
        @closeModal="onCancel" 
        :footer="footer" 
        >
        <template #cusBtn>
          <a-button type="primary" @click="onSure" :loading="loadingr" >确认</a-button>
        </template>
        <template>
          <a-textarea
            v-model="reasonVal"
            placeholder="请输入拒绝理由"
          />
        </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import aListTable from '@/views/app/biz/common/aListTable'
import aListInfo from '@/views/app/biz/common/aListInfo'
import batchOperatBtn from '@/views/app/biz/common/batchOperatBtn'
import {columnsDetail,infoDatas} from './dataConfig'
import { getInfoByPid,getDetailByConditions,getDetailByConditionsCount, confirmOrder, refuseOrder } from './saleOrder'

export default {
  components: {
    basicModal,
    aListTable,
    batchOperatBtn,
    aListInfo
  },
  data() {
    return {
      footer:undefined,
      editable:false,
      loading: false,
      isbatch:false,
      isview:true,
      reason:{
        modalVisible: false,
        modalTitle: '请填写驳回理由',
        modalWidth: '400px',
      },
      loadingr:false,
      loading_p:false,
      reasonVal:'',
      modal: {
        modalVisible: false,
        modalTitle: '订单确认',
        modalWidth: '95vw',
        maxHeight:'70vh'
      },
      infoDatas:infoDatas,
      columns: columnsDetail,
      listdata: [],
      sData: {
        saleOrderPid: null,
      },
      pagination: {
        //分页配置
        current: 1, //当前页码 current
        pageSize: 10, //每页显示条数 pageSize
        total: 1,
        showQuickJumper: true,
        showSizeChanger: true,
        pageSizeOptions: ['1', '5', '10', '100'],
      },
      listKey: 0,
      pid: '',
      sysSeq: 0,
      aRecord:{},
      recordArr:[],
      loadings: false,
    }
  },
  computed: {},
  mounted() {},
  methods: {
    // 点击了上传
    addModal(val) {
      this.modal.modalVisible = true
      this.modal.modalTitle = '确认订单商品'
      this.listdata = []
      this.pid = val
      this.getSBypid(val)
    },
    // 点击了确认订单
    editModal(val,record) {
      this.listdata = []
      this.modal.modalVisible = true
      this.modal.modalTitle = '确认订单'
      this.modal.modalWidth="95vw"
      this.sData.saleOrderPid= record.pid
      this.isview=true
      this.isbatch=false
      this.getSBypid(val)
      this.getDetailBypid(this.sData)
      this.getTotal()
    },
     // 点击了批量确认订单
    batchModal(val) {
      this.recordArr=JSON.parse(JSON.stringify(val))
      this.modal.modalVisible = true
      this.modal.modalTitle = '批量确认订单'
      this.modal.modalWidth="400px"
      this.isview=false
      this.isbatch=true
    },
    // 关闭模态详细框
    closeModal() {
      Object.assign(this.$data, this.$options.data());
      this.$emit('close')
    },
    closeModal2() {
      Object.assign(this.$data, this.$options.data());
      this.$emit('ok')
    },
    //
    onAddData() {
      this.$refs.addSellerLicChildModal.addModal(this.pid)
    },

    // 确认订单通过
    onPassed() {
      this.loading_p=true;
      let reqArr=[]
      if(this.isbatch){
        for (const item of this.recordArr) {
          item.memo=null
          reqArr.push(this.confirm(item))
        }
      }else{
        // let subData = {
        //   sysSeq: this.sysSeq,
        //   pid: this.pid,
        //   memo:null
        // }
        let subData= JSON.parse(JSON.stringify(this.aRecord))
        subData.memo=null
        reqArr.push(this.confirm(subData))
      }
      console.log(reqArr);
      this.dealResult(reqArr,true)
    },
    // 拒单onUnPassed
    onUnPassed(val) {
      this.reason.modalVisible=true;
    },
    //拒单理由确认
    onSure(){
      this.loadingr=true;
      let reqArr=[]
      if(this.isbatch){
        for (const item of this.recordArr) {
          item.memo=this.reasonVal
          item.saleOrderState=this.appState.saleOrderState.refused.code
          reqArr.push(this.refuse(item))
        }
      }else{
        // let subData = {
        //   sysSeq: this.sysSeq,
        //   pid: this.pid,
        //   memo:this.reasonVal
        // }
        let subData= JSON.parse(JSON.stringify(this.aRecord))
        subData.memo=this.reasonVal
        subData.saleOrderState=this.appState.saleOrderState.refused.code
        reqArr.push(this.refuse(subData))
      }
      this.dealResult(reqArr,false)
      
    },
    //拒单理由取消
    onCancel(){
      this.reasonVal=null;
      this.loadingr=false;
      this.loading_p=false;
      this.reason.modalVisible=false;
    },
    // 确认
    async confirm(val) {
      console.log(val);
      let res = await confirmOrder(val)
      return res
    },
    // 拒单
    async refuse(val) {
      console.log(val);
      let res = await refuseOrder(val)
      return res
    },
    // 保存详细信息
    dealResult(reqArr,isPass) {
      let tips=''
      let errTips=''
      if(isPass){
        tips='已成功确认'
        errTips='确认失败，请稍后再试'
      }else{
        tips='已拒单'
        errTips='拒单失败，请稍后再试'
      }
      return Promise.all(reqArr).then(res => {
        this.loading = false
        console.log(res)
        for (const item of res) {
          if (item && item.code && item.code === 'C1') {
            this.$message.success({ content: tips, duration: 2 })
          } else {
            this.$message.error({ content: errTips, duration: 2 })
          }
        }
        this.onCancel()
        this.closeModal2()
      }).catch(err => {
        console.log(err);
        this.$message.error({ content:'请求出错:'+err, duration: 3 })
        this.onCancel()
      })
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        this.sysSeq = res.data.sysSeq
        this.aRecord=res.data
        for (const key in res.data) {
            for (const item of this.infoDatas) {
                if(key==item.id){
                    item.value=res.data[key]
                }
            }
        }
      }
    },
    // 触发改变页码,筛选,排序时的回调
    handleTableChange(pagination, filters, sorter) {
      this.pagination.current = pagination.current
      this.pagination.pageSize = pagination.pageSize
      this.getDetailBypid(this.sData)
    },
    // 初始化页面时得到总数
    async getTotal() {
      let pages = ''
      pages = await getDetailByConditionsCount(this.sData)
      console.log(pages)
      if (pages && pages.code && pages.code === 'C1') {
        const pagination = { ...this.pagination }
        pagination.total = pages.data
        this.pagination = pagination
        this.pagination.showTotal =  (showTotal) => {return `共 ${showTotal} 条`}
      }
    },
    //得到初始化订单商品list
    async getDetailBypid(params) {
      console.log('请求列表时传入的数据', params)
      this.loading = true
      let listdata = []
      let res = ''
      res = await getDetailByConditions(params, this.pagination)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        const resdata = res.data
        this.orgList=res.data
         for (let item of resdata) {
            listdata.push({
               key: item.pid,
               ...item
               }
            )
         }
         this.listdata =listdata
      } else {
        this.listdata = []
      }
    },
  },
}
</script>

<style>
.recordBox {
  width: 100%;
  min-height: 80px;
}
</style>